<?php

namespace Home\Controller;

header("Content-Type:text/html; charset=UTF-8");

class MyController extends HomeCommonController
{
	/**
	*	成为会员
	*/ 
	public function upgradeMember()
	{
	 	$member_id = ''? NULL:I('id');
	 	if (empty($member_id)) {
            $code = self::ERROR_CODE;
            $message = self::PARAM_ERROR;
            $this->showMessage(array(), $code, $message);  
        }

 		$model = M('member');
		$member = $model->getById($member_id); 

		//intval($member['is_member']) > 0
		if($member['is_member'] === '1')
		{
			$code = self::ERROR_CODE;
            $message = '您已经是会员身份';
            $this->showMessage(array(), $code, $message);			
		}
		$vip = intval(self::FREEZE_COIN);//购买会员所使用的金币
		//积分兑换部分
		$score_coin = intval($member['score']/10);
		//var_dump($score_coin);exit;
		$coinCount = intval($member['coin']-$vip+$score_coin);
		if($coin<0)
		{
			$code = self::ERROR_CODE;
            $message = '余额不足';
            $this->showMessage(array(), $code, $message);	
		}

		$tranDb = M();
		$tranDb->startTrans();

		//初始化积分，金币表更新状态为true
		$coin = false;
		$score = false;


        //如果积分是0
        if (intval($member['score'])==0) {
        	$memberdata['freeze_coin'] = $vip;
        	$memberdata['coin'] = intval($member['coin'] - $vip);
        	$coin = false;
        	$coinChange = 0 - intval($vip);
        }else if (intval($member['score']) >= $vip * 10) {//积分够兑换vip
          $memberdata['score'] = intval($member['score'] - $vip * 10);	
          $score = false;
          $scoreChange = 0 - intval($vip * 10);
        }else{//使用积分抵消部分金币
        	$memberdata['freeze_coin'] = intval($vip-$score_coin);
        	$memberdata['coin'] = $coinCount;
			$memberdata['score'] = intval($member['score'] - $score_coin*10);
			$coin = false;
			$score = false;
			$coinChange = 0-intval($vip-$score_coin);
			$scoreChange = 0 - intval($score_coin*10);

        }
        $memberdata['member_date'] = NOW_TIME;
        $memberdata['is_member'] = 1;
        //$memberdata['match_flg'] = 1;
        $memberupdate = M('member')->where(array('id' => $member_id))->save($memberdata);
        if (!$coin) {
  			$coindata = array(
            	'member_id' => $member_id,
             	'coin' => $coinChange,
             	'create_time' => NOW_TIME,
             	'type' => 3//兑换vip
        	);    
        	//会员金币表
        	$coin = M('coin')->add($coindata);  	
        }
        if(!$score)
        {
        	$scoredata = array(
        		'member_id' => $member_id,	
        		'score' => $scoreChange,
        		'create_time' => NOW_TIME,
        		'type' => 3
        	);	
        	$score = M('score')->add($scoredata);
        }

        if ($coin !== false && $memberupdate !== false && $score !== false)
        {
        	$tranDb->commit();
          	$this->showMessage(array());
        }else{
        	$tranDb->rollback();
          	$this->showMessage(array(),self::ERROR_CODE,self::ERROR_MSG);	
        }

	}
	/**
	*	会员充值
	*/ 	
	public function recharge()
	{
	 	$member_id = ''? NULL:I('id');
	 	//var_dump($member_id);exit;
	 	if (empty($member_id)) {
            $code = self::ERROR_CODE;
            $message = self::PARAM_ERROR;
            $this->showMessage(array(), $code, $message);  
        }

	 	$coin_count = ''? NULL:I('coin');
	 	if (empty($coin_count)) {
            $code = self::ERROR_CODE;
            $message = self::PARAM_ERROR;
            $this->showMessage(array(), $code, $message);  
        }

        $model = M('member');
		$member_history = $model->getById($member_id); 
		$tranDb = M();
		$tranDb->startTrans();
		if($member_history['is_member'] === '0')//非会员充值
		{
			$memberdata['coin'] = intval($member_history['coin']+$coin_count);	
		}else{
			$memberdata['coin'] = intval($member_history['coin']+$coin_count);
			$memberdata['score'] = intval($member_history['score']+$coin_count);
		}
		//会员表
        $member = M('member')->where(array('id' => $member_id))->save($memberdata);

        $coindata = array(
             'member_id' => $member_id,
             'coin' => $coin_count,
             'create_time' => NOW_TIME,
             'type' => 1//充值
        );
        //会员金币表
        $coin = M('coin')->add($coindata); 

        if($member_history['is_member'] === '0')//非会员充值  不给积分
        {
        	$score = true;	
        }else{
        	$scoredata = array(
        		'member_id' => $member_id,	
        		'score' => $coin_count,
        		'create_time' => NOW_TIME,
        		'type' => 1
        	);	
        	$score = M('score')->add($scoredata);
        }    

         if ($coin !== false &&
            $member !== false && $score !== false
        ) {
            $tranDb->commit();
            $result = true;
        } else {
            $tranDb->rollback();
            $result = false;
        }

  	    if ($result) {
            $this->showMessage(array());
        } else {
            $this->showMessage(array(),self::ERROR_CODE,self::ERROR_MSG);
        }  
	}
	// public function recharge()
	// {
	//  	$member_id = ''? NULL:I('id');
	//  	//var_dump($member_id);exit;
	//  	if (empty($member_id)) {
 //            $code = self::ERROR_CODE;
 //            $message = self::PARAM_ERROR;
 //            $this->showMessage(array(), $code, $message);  
 //        }

	//  	$coin_count = ''? NULL:I('coin');
	//  	if (empty($coin_count)) {
 //            $code = self::ERROR_CODE;
 //            $message = self::PARAM_ERROR;
 //            $this->showMessage(array(), $code, $message);  
 //        }

 //        $model = M('member');
	// 	$member_history = $model->getById($member_id); 
	// 	$vip = intval(self::FREEZE_COIN);//购买会员所使用的金币

 //        $tranDb = M();
 //        $tranDb->startTrans();

 //        
 //        if($member_history['is_member'] === '0')//非会员充值
 //        {
 //        	if(intval($member_history['coin']+$coin_count)>=$vip)//充值数量满足vip
 //        	{
 //        		$memberdata['freeze_coin'] = $vip;
 // 				$memberdata['coin'] = intval($member_history['coin']+$coin_count-$vip);
 // 				$memberdata['score'] = intval($member_history['coin']+$coin_count-$vip);
 // 				$memberdata['is_member'] = 1;
 // 				$memberdata['member_date'] = NOW_TIME;
 //        	}else{
 //        		$memberdata['coin']	= $coin_count;
 //        	}
 //        }else{
 //        	$memberdata['coin'] = intval($member_history['coin']+$coin_count);
 //        	$memberdata['score'] = intval($member_history['score']+$coin_count);
 //        }
 

 //        //会员表
 //        $member = M('member')->where(array('id' => $member_id))->save($memberdata);

 //        $coindata = array(
 //             'member_id' => $member_id,
 //             'coin' => $coin_count,
 //             'create_time' => NOW_TIME,
 //             'type' => 2//充值
 //        );
 //        //会员金币表
 //        $coin = M('coin')->add($coindata);

 //        if ($coin !== false &&
 //            $member !== false
 //        ) {
 //            $tranDb->commit();
 //            $result = true;
 //        } else {
 //            $tranDb->rollback();
 //            $result = false;
 //        }

 //  	    if ($result) {
 //            $this->showMessage(array());
 //        } else {
 //            $this->showMessage(array(),self::ERROR_CODE,self::ERROR_MSG);
 //        }      
	//  }
	/**
	*	会员信息
	*/ 
	public function myInformation()
	{
		$member_id = ''? NULL:I('id');	
		if (empty($member_id)) {
            $code = self::ERROR_CODE;
            $message = self::PARAM_ERROR;
            $this->showMessage(array(), $code, $message);  
        }
		$model = M('member');
		$list = $model->getById($member_id);
		if ($list) {
            $this->showMessage($list);
        }else{
            $code = self::ERROR_CODE;
            $message = self::DATA_EMPTY;
            $this->showMessage(array(), $code, $message);
        } 
	}
	/**
     * 我的比赛信息
    */   
    public function matchInformation()
    {
        $member_id = ''? NULL:I('id');
        if (empty($member_id)) {
            $code = self::ERROR_CODE;
            $message = self::PARAM_ERROR;
            $this->showMessage(array(), $code, $message);  
        }
        $model=M()->query("SELECT m.name,m.start,m.end,m.status,c.code,r.rank FROM ddz_match_no n 
        LEFT JOIN ddz_match m ON n.match_id = m.id 
        LEFT JOIN ddz_match_code c ON n.match_id = c.match_id AND n.member_id = c.member_id 
        LEFT JOIN ddz_match_result r ON n.match_id = r.match_id AND n.member_id = r.member_id 
        where n.member_id = '$member_id'");

        foreach ($model as $k => $v) {
              $model[$k]['status'] = C('matchStatus')[$v['status']];  
            }
        if ($model) {
            $this->showMessage($model);
        }else{
            $code = self::ERROR_CODE;
            $message = self::DATA_EMPTY;
            $this->showMessage(array(), $code, $message);
        }     
    }
	public function index()
    {
    	$this->display();
    }
}
